package j2.shopmail.ui.home;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import j2.shopmail.*;
import j2.shopmail.ui.dashboard.DashboardFragment;

import java.util.ArrayList;

public class HomeFragment extends Fragment
{
    
    Banner banner;
    ArrayList<Bitmap> imgs;
    AsycHttpAction<String,Bitmap> action= new AsycHttpAction<String, Bitmap>() {
        @Override
        public Bitmap BackGroundAction(String... params)
        {
            byte[] data = ImageHttpHelper.getImage(params[0]);
            //把二进制图片转成位图
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            return bitmap;
        }
        
        @Override
        public void ProgressUpdateAction(View UI, Double... values)
        {
        
        }
        
        @Override
        public void ResultFunction(Bitmap result)
        {
            imgs.add(result);
            banner.setImageLoader(new GlideImageLoader());
            banner.setImages(imgs);
            banner.start();
        }
        
        @Override
        public void PreDoFunction()
        {
        
        }
    };
    class GlideImageLoader extends ImageLoader
    {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView)
        {
            Bitmap bitmap = (Bitmap)path ;
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageBitmap(bitmap);
        }
    }
    static ListView goodslist;
    class ListAdp extends BaseAdapter
    {
       public ArrayList<Goods> list = new ArrayList<>();
        @Override
        public int getCount()
        {
            return list.size();
        }
    
        @Override
        public Object getItem(int position)
        {
            return list.get(position);
        }
    
        @Override
        public long getItemId(int position)
        {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
           View view= LayoutInflater.from(MainActivity.Self).inflate(R.layout.goodslistitem, null);
            TextView name=view.findViewById(R.id.GoodsName);
            TextView price=view.findViewById(R.id.Price);
            ImageView img=view.findViewById(R.id.ItemImage);
           Button btn=view.findViewById(R.id.AddTO);

           final Goods item= list.get(position);
            OrderGood i=Find(item.ID);
            OrderGood good=new OrderGood();
            if(i!=null)
            {
                btn.setText("加入购物车\n(现有"+i.getCount()+"个)");
            }
            item.imgTarget=img;
           name.setText(item.Name);
           price.setText(String.valueOf( item.Price));
           img.setImageBitmap(item.Image);
           btn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v)
               {
                   
                   OrderGood good=new OrderGood();
                   good.setGoodID(item.ID);
                   good.setCount(1);
                   Button button=(Button) v;
                   OrderGood i=Find(item.ID);
                   if(i!=null)
                   {
                       i.setCount(i.getCount()+1);
                       button.setText("加入购物车\n(现有"+i.getCount()+"个)");
                   }
   
                   else
                   {
                       DashboardFragment.orderGoods.add(good);
                       button.setText("加入购物车\n(现有"+good.getCount()+"个)");
                   }

                   
                   
               }
           });
            return view;
        }
        OrderGood Find(String id)
        {
                for(int i=0;i< DashboardFragment.orderGoods.size();i++)
                {
                    if(DashboardFragment.orderGoods.get(i).getGoodID()!=null)
                    if(DashboardFragment.orderGoods.get(i).getGoodID().trim().equals(id.trim()))
                        return DashboardFragment.orderGoods.get(i);
                }
                return null;

        }
    }
     static ListAdp arrayList;
    public  static  void AddItem(Goods i)
    {
        if(arrayList!=null)
        arrayList.list.add(i);
        if(goodslist!=null)
            goodslist.setAdapter(arrayList);
    }
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        banner = root.findViewById(R.id.banner);
        imgs=new ArrayList<>();
       goodslist= root.findViewById(R.id.GoodsList);
        arrayList = new ListAdp();
        AsycHttp<Object,Integer> getcount=new AsycHttp<>();
        getcount.setBackgroundAction(new AsycHttpAction<Object, Integer>() {
            @Override
            public Integer BackGroundAction(Object... params)
            {
                try
                {
                    String p= HttpHelper.Get(ServerHelper.MakeGoodCountStr()).trim();
                
                    return Integer.valueOf(p);
                }catch(Exception ex)
                {
                    System.out.print(ex.getStackTrace());
                    return 0;
                }
            
            }
        
            @Override
            public void ProgressUpdateAction(View UI, Double... values)
            {
            
            }
        
            @Override
            public void ResultFunction(Integer result)
            {
                ListAdp arrayList = new ListAdp();
                Goods.NowGoods.clear();
                for(int i=1;i<=result;i++)
                {
                   Goods.NowGoods.add(new Goods(i));
                }
                goodslist.setAdapter(arrayList);
            }
        
            @Override
            public void PreDoFunction()
            {
            
            }
        });
        getcount.execute();
        for(int i=1;i<=2;i++)
        {
            AsycHttp<String,Bitmap> imghttp=new AsycHttp<>();
            imghttp.setBackgroundAction(action);
            imghttp.execute(ServerHelper.MakeGetImageStr("b"+String.valueOf(i)));
        }
        return root;
    }
}